package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.entity.SysRole;
import com.cy.pj.sys.entity.SysRoleMenuVo;
import com.cy.pj.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    /**
     * 按角色名和当前页数获取系统角色信息
     * @param name 角色名
     * @param pageCurrent 当前页数
     * @return 系统角色信息
     */
    @Override
    public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {
        if(pageCurrent == null || pageCurrent < 1)
            throw new IllegalArgumentException("当前页码值无效");
        int rowCount = sysRoleDao.getRowCount(name);
        if(rowCount == 0)
            throw new ServiceException("没有找到对应的记录");
        int pageSize = 2;
        int startIndex = (pageCurrent - 1) * pageSize;// 分页查询的起始位置：计算出来的
        List<SysRole> records = sysRoleDao.findPageObjects(name, startIndex, pageSize);
        return new PageObject<SysRole>(rowCount, pageSize, pageCurrent, records);
    }

    @Override
    public int deleteObject(Integer id) {
        if(id == null || id < 0)
            throw new IllegalArgumentException("请先选择");
        // 基于角色ID删除用户角色(sys_user_roles)和角色菜单(sys_role_menus)
        sysUserRoleDao.deleteObjectsByRoleId(id);
        sysRoleMenuDao.deleteObjectsByRoleId(id);
        // 基于ID删除角色表(sys_roles)
        int rows = sysRoleDao.deleteObject(id);
        if(rows == 0)
            throw new ServiceException("此记录可能已经不存在");
        return rows;
    }

    @Override
    public int saveObject(SysRole entity, Integer[] menuIds) {
        if(entity == null)
            throw new IllegalArgumentException("请完善角色信息");
        if(entity.getName() == null || entity.getName() == "")
            throw new IllegalArgumentException("角色名称不能为空");
        if(menuIds == null || menuIds.length == 0)
            throw new ServiceException("必须为角色分配权限");
        int rows = sysRoleDao.insertObject(entity);
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return rows;
    }

    @Override
    public SysRoleMenuVo findObjectById(Integer id) {
        // 参数校验
        if(id == null || id < 1)
            throw new IllegalArgumentException("id的值不合法");
        SysRoleMenuVo result = sysRoleDao.findObjectById(id);
        if(result == null)
            throw new ServiceException("此记录已经不存在");
        return result;
    }

    @Override
    public int updateObject(SysRole entity, Integer[] menuIds) {
        if(entity == null)
            throw new IllegalArgumentException("更新的对象不能为空");
        if(entity.getId() == null)
            throw new IllegalArgumentException("id的值不能为空");
        if(entity.getName() == null || entity.getName() == "")
            throw new IllegalArgumentException("角色名称不能为空");
        if(menuIds == null || menuIds.length == 0)
            throw new IllegalArgumentException("必须为角色指定权限");
        int rows = sysRoleDao.updateObject(entity);
        if(rows == 0)
            throw new ServiceException("对象可能已经不存在");
        sysRoleMenuDao.deleteObjectsByRoleId(entity.getId());
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return rows;
    }

    @Override
    public List<SysRole> findRoles() {
        List<SysRole> list = sysRoleDao.findRoles();
        return list;
    }
}
